<%--
  Created by IntelliJ IDEA.
  User: Adam
  Date: 2022/3/10
  Time: 12:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<!--开启EL表达式-->
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#btn").click(function(){
                //alert("button click");
                $.ajax({
                    //url:"returnVoid-ajax.do",
                    // url:"/ch04-return/user/returnStudentJson.do",
                    // url:"/ch04-return/user/returnStudentJsonArray.do",
                    url:"/ch04-return/user/returnStringData.do",
                    data:{
                        name:"zhangsan",
                        age:20
                    },
                    type:"post",
                    dataType:"text",
                    // dataType:"json",
                    success:function(resp){
                        //resp从服务器端返回的是json格式的字符串 {"name":"zhangsan","age":20}
                        //jquery会把字符串转为json对象， 赋值给resp形参。
                        // alert(resp.name + "    "+resp.age);

                        // [{"name":"李四","age":4},{"name":"张三","age":3}]
                        // $.each(resp,function(i,n){
                        //     alert(n.name+"   "+n.age)
                        // })

                        alert("返回的是文本数据："+resp);

                    }
                })
            })
        })
    </script>
</head>
<body>


<p>处理器方法返回string表示视图名称</p>

<form action="/ch04-return/user/returnString.do" method="post">
    姓名：<input type="text" name="name"> <br>
    年龄：<input type="text" name="age"> <br>
    <input type="submit" value="提交参数">
</form>

<br>
<button id="btn">发起ajax请求</button>

</body>
</html>
